package com.chen.pfm.utils;

import com.chen.pfm.entity.Statements;
import ohos.aafwk.ability.DataAbilityHelper;
import ohos.aafwk.ability.DataAbilityRemoteException;
import ohos.app.Context;
import ohos.data.dataability.DataAbilityPredicates;
import ohos.data.rdb.ValuesBucket;
import ohos.data.resultset.ResultSet;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.utils.net.Uri;

import java.util.ArrayList;
import java.util.List;

public class StatementsDBUtil {
    static final HiLogLabel LABEL = new HiLogLabel(HiLog.LOG_APP, 0x2022001, "StatementsDBUtil");
    static final String uriString = "dataability:///com.chen.pfm.StatementsDataAbility/statements";

    public static boolean isExistsMonthStatementsBySid(Context context, String Sid) {
        boolean flag = false;
        DataAbilityHelper helper = DataAbilityHelper.creator(context);
        // 构造查询条件
        DataAbilityPredicates predicates = new DataAbilityPredicates();
        predicates.equalTo("sid", Sid);
        String[] columns = new String[]{"sid"};
        Uri uri = Uri.parse(uriString);
        //数据库帮助类调用查询，得到结果集
        ResultSet resultSet = null;
        try {
            resultSet = helper.query(uri, columns, predicates);
            if (resultSet.getRowCount() > 0) {
                flag = true;
            }
        } catch (DataAbilityRemoteException e) {
            e.printStackTrace();
        }
        return flag;

    }
    //查询本月报表
    public static Statements queryMonthStatements(Context context, int finduid, String findsdate) {
        Statements statements = null;
        DataAbilityHelper helper = DataAbilityHelper.creator(context);
        // 构造查询条件
        DataAbilityPredicates predicates = new DataAbilityPredicates();
        predicates.equalTo("uid", finduid);
        predicates.beginsWith("sdate", findsdate);

        String[] columns = new String[]{"sid", "uid", "budget", "sdate", "sctime"};
        Uri uri = Uri.parse(uriString);
        try {
            //数据库帮助类调用查询，得到结果集
            ResultSet resultSet = helper.query(uri, columns, predicates);
            //如果结果集合不为空
            if (resultSet.getRowCount() > 0) {
                // 处理结果
                resultSet.goToFirstRow();
                String sid = resultSet.getString(0);
                int uid = resultSet.getInt(1);
                double budget = resultSet.getDouble(2);
                String sdate = resultSet.getString(3);
                String sctime = resultSet.getString(4);
                statements = new Statements(sid, uid, budget, sdate, sctime);
                // 在此处理ResultSet中的记录;
            } else {
                return null;
            }
        } catch (DataAbilityRemoteException e) {
            e.printStackTrace();
        }
        return statements;
    }

    //得到当前预算
    public static double statisticsCurrentMonthBudgetByUid(Context context, int uid) {
        double currentMonthBudget = 0.0;
        DataAbilityHelper helper = DataAbilityHelper.creator(context);
        // 构造查询条件
        DataAbilityPredicates predicates = new DataAbilityPredicates();
        String strCurrentYearandMonth = DateUtils.getStrCurrentYearandMonth();
        predicates.beginsWith("sdate", strCurrentYearandMonth);
        predicates.equalTo("uid", uid);
        String[] columns = new String[]{"budget"};
        Uri uri = Uri.parse(uriString);
        try {
            //数据库帮助类调用查询，得到结果集
            ResultSet resultSet = helper.query(uri, columns, predicates);
            //如果结果集合不为空
            if (resultSet.getRowCount() > 0) { // 处理结果
                resultSet.goToFirstRow();
                currentMonthBudget = resultSet.getDouble(0);
            }
        } catch (DataAbilityRemoteException e) {
            e.printStackTrace();
        }
        return currentMonthBudget;
    }



    public static int update(Context context, Statements statements) throws DataAbilityRemoteException {
        // 构造插入数据
        DataAbilityHelper helper = DataAbilityHelper.creator(context);
        // 构造更新条件
        DataAbilityPredicates predicates = new DataAbilityPredicates();
        if (statements == null) {
            return -1;
        }
        predicates.equalTo("sid", statements.getSid());
        Uri uri = Uri.parse(uriString);
        int update = helper.update(uri, statements.getValuesBucketfromObject(), predicates);
        return update;
    }


    public static int insert(Context context, Statements statements) {
        int i = -1;
        DataAbilityHelper helper = DataAbilityHelper.creator(context);
        Uri uri = Uri.parse(uriString);
        ValuesBucket valuesBucket = statements.getValuesBucketfromObject();
        try {
            i = helper.insert(uri, valuesBucket);
        } catch (DataAbilityRemoteException e) {
            e.printStackTrace();
        }
        return i;
    }

    public static int delete(Context context, Statements statement) throws DataAbilityRemoteException {
        DataAbilityHelper helper = DataAbilityHelper.creator(context);
        Uri uri = Uri.parse(uriString);
        // 构造删除条件
        DataAbilityPredicates predicates = new DataAbilityPredicates();
        predicates.equalTo("sid", statement.getSid());
        int i = helper.delete(uri, predicates);
        return i;
    }
    public static int deleteByUid(Context context, Integer uid) throws DataAbilityRemoteException {
        DataAbilityHelper helper = DataAbilityHelper.creator(context);
        Uri uri = Uri.parse(uriString);
        // 构造删除条件
        DataAbilityPredicates predicates = new DataAbilityPredicates();
        predicates.equalTo("uid", uid);
        int i = helper.delete(uri, predicates);
        return i;
    }
}
